<?php

namespace App\Http\Controllers\Home;

use App\Model\Globals;
use App\Model\Users;
use App\Models\Archive\Archive;
use App\Models\Tag\Tag;
use App\Model\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class IndexController extends Controller
{
    //
    public function index()
    {
        //title

        $global = Globals::first();
        $view = view('home.index.index');
        //置顶
        $top = explode(',',$global['mod_top1']);
        $top1[0] = Archive::where('id',$top[0])->first();
        $top1[1] = Archive::where('id',$top[1])->first();
        $top1[2] = Archive::where('id',$top[2])->first();
        $top1[3] = Archive::where('id',$top[3])->first();
        $top1[4] = Archive::where('id',$top[4])->first();
        $top2 = Archive::whereIn('id',explode(',',$global['mod_top2']))->get();


        $anrid = explode(',',$global['mod_anchor']);
        $anchor[0] = Tag::where('id', $anrid[0])->first();
        $anchor[1] = Tag::where('id', $anrid[1])->first();
        $anchor[2] = Tag::where('id', $anrid[2])->first();
        $anchor[3] = Tag::where('id', $anrid[3])->first();
        $anchor[4] = Tag::where('id', $anrid[4])->first();


        //视频
        $vid = explode(',',$global['mod_video']);
        $video[0] = Archive::where('id',$vid[0])->first();
        $video[1] = Archive::where('id',$vid[1])->first();
        $video[2] = Archive::where('id',$vid[2])->first();
        $video[3] = Archive::where('id',$vid[3])->first();
        $video[4] = Archive::where('id',$vid[4])->first();
        $video[5] = Archive::where('id',$vid[5])->first();


        $arc1 = explode(',',$global['mod_article1']);
        $article1[0] = Archive::where('id',$arc1[0])->first();
        $article1[1] = Archive::where('id',$arc1[1])->first();
        $article1[2] = Archive::where('id',$arc1[2])->first();
        $article1[3] = Archive::where('id',$arc1[3])->first();
        $article1[4] = Archive::where('id',$arc1[4])->first();
        $article1[5] = Archive::where('id',$arc1[5])->first();
        $article1[6] = Archive::where('id',$arc1[6])->first();
        $article1[7] = Archive::where('id',$arc1[7])->first();


        $arc2 = explode(',',$global['mod_article2']);
        $article2[0] = Archive::where('id',$arc2[0])->first();
        $article2[1] = Archive::where('id',$arc2[1])->first();
        $article2[2] = Archive::where('id',$arc2[2])->first();
        $article2[3] = Archive::where('id',$arc2[3])->first();
        $article2[4] = Archive::where('id',$arc2[4])->first();
        $article2[5] = Archive::where('id',$arc2[5])->first();
        $article2[6] = Archive::where('id',$arc2[6])->first();
        $article2[7] = Archive::where('id',$arc2[7])->first();


        //图库
        $imgid = explode(',',$global['mod_gallery']);
        $gallery[0] = Archive::where('id',$imgid[0])->first();
        $gallery[1] = Archive::where('id',$imgid[1])->first();
        $gallery[2] = Archive::where('id',$imgid[2])->first();
        $gallery[3] = Archive::where('id',$imgid[3])->first();
        $gallery[4] = Archive::where('id',$imgid[4])->first();
        $gallery[5] = Archive::where('id',$imgid[5])->first();
        $gallery[6] = Archive::where('id',$imgid[6])->first();
        $gallery[7] = Archive::where('id',$imgid[7])->first();

        $p_cate = Category::where('cate_name', '直播')->first();
        $cates = Category::where('cate_pid', $p_cate->cate_id)->get();


        //wap导航
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")){
            $mobile = 1;
        }else{
            $mobile = 0;
        }




        $view->with('top1',$top1)
            ->with('top2',$top2)
            ->with('anchor',$anchor)
            ->with('video',$video)
            ->with('article1',$article1)
            ->with('article2',$article2)
            ->with('gallery',$gallery)
            ->with('global',$global)
            ->with('anchor_cates', $cates)
            ->with('mobile', $mobile)
        ;
        return $view;
    }

    private function getArchive($pattern, $take = 4)
    {
        return Archive::whereRaw("(`mode` & $pattern) = $pattern")
            ->take($take)
            ->orderBy('updated_at', 'desc')
        ;
    }

    public function author(Users $user)
    {
        //他的标签
        $archive = Archive::where('user_id',$user->id)->orderBy('updated_at','desc')->take(4)->pluck('id');

        $tag_id = DB::table('archive_tag')->whereIn('archive_id',$archive)->distinct()->pluck('tag_id');
        $tags = Tag::whereIn('id',$tag_id)->distinct()->get();
        $type = Input::get('type')?Input::get('type'):'';

        if($type == 'article'){
            $archives =  Archive::where('user_id',$user->id)->where('archive_type_id',1)->ofPattern('review')->orderBy('updated_at','desc')->paginate(8);
        }elseif($type == 'gallery'){
            $archives =  Archive::where('user_id',$user->id)->where('archive_type_id',2)->ofPattern('review')->orderBy('updated_at','desc')->paginate(8);
        }elseif($type == 'video'){
            $archives =  Archive::where('user_id',$user->id)->where('archive_type_id',3)->ofPattern('review')->orderBy('updated_at','desc')->paginate(8);
        }else{
            $archives = Archive::where('user_id',$user->id)->ofPattern('review')->orderBy('updated_at','desc')->paginate(8);
        }
        return view('home.author.index',compact('user','tags','type'))->with('archives',$archives->appends(Input::only(['type'])));
    }

    public function topSlide($view)
    {
        return $view->with('slides', Archive::ofPattern('review')->orderBy('updated_at', 'desc')->take(6)->get());
    }

    public function special($id)
    {
        return view('home.special.'.$id);
    }
}